import { queryBasicProfile, queryAdvancedProfile } from '../services/api';
import { queryCurrentUserInfo } from '../services/user';
import { getToken } from '../utils/token';
import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { fakeSubmitForm } from '../services/api';

export default {
  namespace: 'profile',

  state: {
    basicGoods: [],
    advancedOperation1: [],
    advancedOperation2: [],
    advancedOperation3: [],
    info:{},
    description:[
      {property:'name',term:"名字"},
      {property:'petName',term:"昵称",value:''},
      {property:'gender',term:"性别",value:''},
      {property:'birthday',term:"生日",value:''},
      {property:'location',term:"所在地",value:''},
      {property:'hometown',term:"家乡",value:''},
      {property:'resume',term:"个性签名",value:''},
      {property:'profession',term:"职业",value:''},
    ]
  },

  effects: {
    *fetchBasic(_, { call, put }) {
      const response = yield call(queryBasicProfile);
      yield put({
        type: 'show',
        payload: response,
      });
    },
    *submitRegularForm({ payload }, { call }) {
      yield call(fakeSubmitForm, payload);
      message.success('提交成功');
    },
    
    *fetchCurrentUserInfo(_, { call, put }) {
      const payload={
        token:getToken()
      }
      const response = yield call(queryCurrentUserInfo,payload);
      yield put({
        type: 'saveCurrentUserInfo',
        payload: response,
      });
    },
    *editCurrentUserInfo(_, { call, put }) {
      yield put(routerRedux.push('/personal/edit/profile'));
    },
    *fetchAdvanced(_, { call, put }) {
      const response = yield call(queryAdvancedProfile);
      yield put({
        type: 'show',
        payload: response,
      });
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveCurrentUserInfo(state, action) {
      return {
        ...state,
        info: action.payload,
      };
    },
  },
};
